<?php
class Member extends CI_Controller 
{
     
	  function __construct()
	{
		parent::__construct();	
	    $this->load->model('member_model');
		$this->load->model('category_model');
		$this->load->model('product_model');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index()
	{
		echo "fff";
		$data['title']="Admin dashboard";
		//$this->load->view('header',$data);
	    //$this->load->view('menu');
		//$this->load->view('dashboard');
		//$this->load->view('footer');
	
	}
	function login($st='')
	{
		$username		= $this->input->post('email_id');
		$password		= $this->input->post('password');
		$login          = $this->input->post('save');
		$status         = $this->member_model->login($username,$password);
		if(!empty($login))
		{
			if($status) 
			{
				$mylogin 		= $this->session->userdata('mylogin');
				if(empty($_REQUEST['surl'])) 
					redirect($this->config->item('base_url').'store/products','location');
				else 
					redirect($_REQUEST['surl'],'location');
				exit;
			}
			else
			{
				$view['err_msg']	="Login failed";
			}
		}
		elseif($st=='out')
		{
			$view['st_msg']	="You are logged out now";
		}
		elseif($st=='active')
		{
			$view['st_msg']	="Your account is activated. You can login now.";
		}
		elseif($st=='non')
		{
			$view['st_msg']	="Your account is not activated. You can try again.";
		}
		elseif($st=='cart')
		{
			$view['st_msg']	="Please login to add items to cart.";
		}
		$view['surl'] = $_REQUEST['surl'];

		
		$this->layout->getView('header');
		$this->load->view('login', $view);
		$this->load->view('footer');
	}
	
	function reg($st='')
	{
	
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "form Details";
		$save                    = $this->input->post('save');
		$view['form'] 	         = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Register';
		$view['status']          = $st;
		if(!empty($save)) 
		{
		   
			$data                     		= $this->retrieve->post($this->member_model->fields); 
			$repassword               		= $this->input->post('repassword');
			$data['pass']                   = $this->input->post('password');
			$data['created']          		= date('Y-m-d');
			//$data['non_enc_password'] = $this->input->post('password');
			$view['member']            		= $data;
			$view['member']['repassword']	= $repassword;
			if($data['password']==$repassword)   
			{
			$data['address']      = $data['addressline1']." ".$data['addressline2']." ".$data['city']." ".$data['state']." ".$data['pincode'];
			$insertStatus             		= $this->member_model->add($data);
			
			
			
			switch($insertStatus['status'])
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'registration_add_success');
						
					$this->sentMail($data['email_id'],$insertStatus['id']);
					redirect($this->config->item('base_url').'member/reg/1','location');
					exit;	
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('form_add_failure');
					break;
			}
			}
			else
			{
				$view['mismatch'] =1;
			}  // password check
		}
		    $this->layout->getView('header');
			$this->load->view('registration', $view);
			$this->load->view('footer');
		
	}
	function sentMail($email='',$id)
	{
	
		/*$this->load->library('email');
		
		$config['charset'] = 'utf-8';
		$config['mailtype'] = 'html';
		$this->email->initialize($config);
		
		$this->email->from('RJS@rjs.com', 'RJS');
		$this->email->to($email);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');

		$this->email->subject('RJS Account');
		$this->email->message('Thanks for your account with RJS');

		//$this->email->send();
		$status = $this->email->send(); */
		$headers  		= 'MIME-Version: 1.0' . "\r\n";
		$headers 	   .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

		// Additional headers
		//$headers .= 'From: Olive Supermarket <rjs@rjsgroup.com>' . "\r\n";
		//$headers .= 'Cc: @example.com' . "\r\n";
		//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
		$link           = $this->config->item('base_url')."member/activate/".$id;
		$mess   		= "Welcome to Olive Supermarket..!<br/> Your account has been successfully created. Please click the following link to activate your account <a href='".$link."'> Activate Account  </a>";
		
		$status			= mail($email,"Olive Supermarket Account",$mess,$headers);
	
		return $status;

		
	}
	function activate($id='')
	{
		$status   = $this->member_model->activate($id);
		if($status)
		{
				redirect($this->config->item('base_url').'member/login/active','location');
		}
		else
		{
			redirect($this->config->item('base_url').'member/login/non','location');
		}
		
		
	}
	
	function forget_password()
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "Forget Password";
		$save                    = $this->input->post('save');
		$email                   = $this->input->post('email_id');
		$view['form'] 	         = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Register';
		$view['status']          = $st;
		if(!empty($save)) 
		{
		    $member               = $this->member_model->getMembers(0,'','',array('users.deleted'=>0,'email_id'=>$email));
			$member               = $member->records[0];
			//print_r($member);
	
			if(!empty($member))
			{
				 $pass                  = $member['pass'];
				 $insertStatus          = $this->email_password($email,$pass);
				if($insertStatus) 
				{
					$view['flag'] =1;
				}
			}
			else
			{
				$view['flag'] =2;
			}
			
		}
		    $this->layout->getView('header');
			$this->load->view('forget_password', $view);
			$this->load->view('footer');
			
	}
	function email_password($email,$pass)
	{
		/*$this->load->library('email');
		
		$config['charset'] = 'utf-8';
		$config['mailtype'] = 'html';
		$this->email->initialize($config);
		
		$this->email->from('RJS@rjs.com', 'RJS');
		$this->email->to($email);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');

		$this->email->subject('RJS Account ');
		$this->email->message('Your password is:'.$pass);

		//$this->email->send();
		$status = $this->email->send();*/
		//$member               = $this->member_model->getMembers(0,'','',array('users.deleted'=>0,'email_id'=>$email));
		//$member               = $member->records['0'];
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

		// Additional headers
		//$headers .= 'From: RJS Group <rjs@rjsgroup.com>' . "\r\n";
		$mess = 'Your password is:'.$pass;
		
		$status=mail($email,"Olive Account Detail",$mess,$headers);
	
		return $status;
		
	}
	
	function logout()
	{
		session_start();
		session_destroy();
		$this->session->sess_destroy();
		redirect($this->config->item('base_url').'member/login/out','location');
		exit;
	}
	
	
	
}
?>